package com.xh.demo.domain.vo;

import com.xh.demo.common.enums.ErrorEnum;
import com.xh.demo.common.message.ICommonMessage;
import lombok.Getter;

/**
 * @Name WebApiResult
 * @Description ajax请求返回结果类
 * @Author wen
 * @Date 2019-08-01
 */
@Getter
public class AJAXResult<T> {

    private int code;

    private String msg;

    private T data;

    public AJAXResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public AJAXResult(int code, String msg, T data) {
        this(code, msg);
        this.data = data;
    }

    public static AJAXResult success(){
        return success(ErrorEnum.SUCCESS.getCode(), ErrorEnum.SUCCESS.getMsg());
    }

    public static AJAXResult success(ICommonMessage cm){
        return success(cm.getCode(), cm.getMsg());
    }

    public static AJAXResult success(int code, String msg){
        return new AJAXResult(code, msg);
    }

    public static <T> AJAXResult success(T data){
        return success(ErrorEnum.SUCCESS.getCode(), ErrorEnum.SUCCESS.getMsg(), data);
    }

    public static <T> AJAXResult success(ICommonMessage cm, T data){
        return success(cm.getCode(), cm.getMsg(), data);
    }

    public static <T> AJAXResult success(int code, String msg, T data){
        return new AJAXResult(ErrorEnum.SUCCESS.getCode(), ErrorEnum.SUCCESS.getMsg(), data);
    }

    public static AJAXResult fail(){
        return fail(ErrorEnum.FAIL.getCode(), ErrorEnum.FAIL.getMsg());
    }

    public static AJAXResult fail(String msg){
        return fail(ErrorEnum.FAIL.getCode(), msg);
    }

    public static AJAXResult fail(ICommonMessage cm){
        return fail(cm.getCode(), cm.getMsg());
    }

    public static AJAXResult fail(int code, String msg){
        return new AJAXResult(code, msg);
    }

    public static AJAXResult result(int res){
        return res > 0 ? success(): fail();
    }

}
